<template>
  <base-layout
    :dialog-title="dialogTitle"
    :dialog-visible="dialogVisible"
    @dialog-close="dialogClose"
  >
    <template #searchPanel>
      <el-row :gutter="20">
        <search-panel
          v-slot="{ form }"
          @search="search"
          :showAddButton="true"
          @insert="addService"
          :addPer="'xcAllot:save'"
          :selectPer="'xcAllot:page'"
          addTitle="新增"
        >
          <!--<el-select-->
            <!--clearable-->
            <!--style="width: 150px"-->
            <!--v-model="form.source"-->
            <!--placeholder="进口来源:"-->
          <!--&gt;-->
            <!--&lt;!&ndash; <el-option label="全部" value=""></el-option> &ndash;&gt;-->
            <!--<el-option-->
              <!--v-for="item in getDict['进口来源']"-->
              <!--:key="item.index"-->
              <!--:value="item.index"-->
              <!--:label="item.text"-->
            <!--/>-->
          <!--</el-select>-->
          <el-select
            clearable
            style="width: 150px"
            v-model="form.storeId"
            placeholder="门店:"
          >
            <!-- <el-option label="全部" value=""></el-option> -->
            <el-option
              v-for="item in stores"
              :key="item.id"
              :value="item.id"
              :label="item.storeName"
            />
          </el-select>

          <el-date-picker
            v-model="form.time"
            type="daterange"
            range-separator="-"
            start-placeholder="申请开始日期"
            end-placeholder="申请结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </search-panel>
      </el-row>
    </template>

    <template #contentPanel>
      <data-table
        :value="tableData"
         :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        :width="160"
        @row-dblclick="rowDblclick"
        :isShowPage="true"
      >
        <template #action="{ row }">
          <i
          v-if="row.sourceStoreId===storeId&&row.reviewStatus==='待审批'"
            class="el-icon-s-claim"
            title="审核"
            v-permission="'xcAllot:sh'"
            style="margin-left: 15px; cursor: pointer"
            @click="applyTransfer(row, 0)"
          ></i>
          <i
            class="el-icon-edit"
            title="修改"
            v-if="row.status!=='审核通过'&&row.storeId===storeId"
            v-permission="'xcAllot:update'"
            style="margin-left: 15px; cursor: pointer"
            @click="interestsInfo(row, 0)"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
              v-if="row.status!=='审核通过'&&row.storeId===storeId"
            v-permission="'xcAllot:del'"
            style="color: red; margin-left: 15px; cursor: pointer"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>

      <!-- <el-dialog title="调拨审核" :visible.sync="dialogVisibleForm" width="30%">
        <el-form
          ref="applyForm"
          :rules="rules"
          :model="applyForm"
          label-width="100px"
        >
          <el-form-item label="审批处理:" prop="status">
            <el-radio-group v-model="applyForm.status">
              <el-radio label="通过">通过</el-radio>
              <el-radio label="不通过">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注:" prop="noSuccessDesc">
            <el-input type="textarea" v-model="applyForm.noSuccessDesc" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer text-center">
          <el-button type="primary" @click="apply('applyForm')"
            >确 定</el-button
          >
        </div>
      </el-dialog> -->
      <ApplyDialog @apply-dialog="apply"/>
    </template>
    <template #dialog>
      <TransferView :dialog-data="dialogData" />
    </template>
  </base-layout>
</template>

<script>
import { getDict,getStoreId } from '@/utils/index'
import baseMixin from '@/mixins/base'
import ApplyDialog from '@/components/ApplyDialog'
import {
  dataList,
  deleteData,
  details,
} from '@/api/pre-sale/newCarStock/transferApply'
import { auditIsOk } from '@/api/common'
import { mapGetters } from 'vuex'
import TransferView from './components/transferView'
export default {
  components: { TransferView,ApplyDialog },
  mixins: [
    baseMixin({
      idName: 'id',
      dataName: ['content', 'totalRecords'],
      findData: dataList,
      deleteData,
      findDataById: details,
      format(res) {
        return res.map((data) => {
          data.rate = data.rate + '%'
          data.invoicingName = data.invoicing ? '是' : '否'
          return data
        })
      },
    }),
  ],
  computed: {
    ...mapGetters(['stores','userInfo']),
  },
  data() {
    return {
      dialogVisibleForm: false,
      dialogTitle: '调拨详情',
      getDict: getDict(['进口来源']),
      storeId: getStoreId(),
      tableColumnAttributes: [
        { name: '调拨单号', value: 'code', type: 'text' },
        { name: '申请门店', value: 'storeName', type: 'text' },
        { name: '来源门店', value: 'sourceStoreName', type: 'text' },
        { name: '调拨原因', value: 'reason', type: 'text' },
        { name: '是否开票', value: 'invoicingName', type: 'text' },
        { name: '车辆数', value: 'carNum', type: 'text' },
        { name: '税率(%)', value: 'rate', type: 'text' },
        { name: '总金额(元)', value: 'amount', type: 'text' },
        { name: '状态', value: 'status', type: 'text' },
        { name: '创建人', value: 'createUserName', type: 'text' },
        { name: '申请时间', value: 'createTime', type: 'text' },
        { name: '备注', value: 'remark', type: 'text' },
        { name: '审核状态', value: 'reviewStatus', type: 'text' },
        { name: '审核不通过原因', value: 'noSuccessDesc', type: 'text' },
      ],
    }
  },
  async created() {
    console.log(getStoreId())
  },
  methods: {
    //审核
    async apply(params) {
      
      const { success } = await auditIsOk({ ...params, sourceCode: this.currentRow.code })
      if (success) {
        this.currentRow = {}
        this.setTableData()
        this.$store.dispatch('others/setDialog', {
          visible: false,
          title: '调拨审核',
        })
        this.$message.success('操作成功')
      }
    },
    // 查看调拨记录
    rowDblclick(row) {
      this.handleDataChange(row)
    },
     search(form) {
       console.log(form.time)
      form.startTime=form.time&&form.time[0]||''
       form.endTime=form.time&&form.time[1]||''
       delete form['time']
      this.searchOptions=form;
      this.currentPage=1;
      this.setTableData()

    },
    interestsInfo(row) {
      this.$router.push({ name: 'editTransferApply', params: { id: row.id } })
    },
    addService() {
      this.$router.push({ name: 'addTransferApply' })
    },
    applyTransfer(row) {
      this.$store.dispatch('others/setDialog', {
        visible: true,
        title: '调拨审核',
      })
      this.currentRow = row
      this.dialogVisibleForm = true
    },
  },
}
</script>

<style lang="scss" scoped>
.vLine {
  border-left: solid 3px black;
  height: 16px;
  vertical-align: bottom;
  display: inline-block;
}
</style>
